/**
 * npm install only -S
 * @description 返回對象的白名單屬性。
 * @param {Object} obj 操作对象
 * @param {string|Array<string>} keys 过滤keys
 * @param {Boolean} flag 白名单(true) or 黑名单(false)
 */

function only(obj = {}, keys, flag = false) {
  // console.log(10, obj, flag);
  if (!(Object.prototype.toString.call(obj) === '[object Object]')) return {};
  const aAllKeys = Object.keys(obj);
  if (aAllKeys.length <= 0) return {};

  let aKeys = keys;
  if (typeof keys === 'string') aKeys = keys.split(/ +/);

  return aAllKeys.reduce((ret, key) => {
    if (!flag && aKeys.indexOf(key) >= 0) return ret;
    if (flag && aKeys.indexOf(key) < 0) return ret;
    // eslint-disable-next-line
    ret[key] = obj[key];
    return ret;
  }, {});
}

module.exports = only;
